package org.springmvc.model;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;

import org.apache.commons.lang.builder.HashCodeBuilder;

/**
 * 基础实体类
 * <p>
 * 所有继承该实体类的实体在映射成表的时候会自动加入该类中的字段
 * 
 * @author Garfield
 */
@MappedSuperclass
public abstract class BaseEntity implements Serializable {
	private static final long serialVersionUID = -5082401234492420871L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer id;
	@Version
	private Integer version;
	/** 记录是否逻辑删除 。true 已删除，false 未删除 */
	private boolean isDeleted = false;
	/** 创建时间 */
	private Date createTime;
	/** 创建人 */
	private Integer createBy;
	/** 修改时间 */
	private Date updateTime;
	/** 修改人 */
	private Integer updateBy;
	/** 删除时间 */
	private Date deleteTime;
	/** 删除人 */
	private Integer deleteBy;
	/** 备注 */
	private String memo;

	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Integer getVersion() {
		return version;
	}
	public void setVersion(Integer version) {
		this.version = version;
	}
	public boolean getIsDeleted() {
		return isDeleted;
	}
	public void setIsDeleted(boolean isDeleted) {
		this.isDeleted = isDeleted;
	}
	public Date getCreateTime() {
		return createTime;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	public Integer getCreateBy() {
		return createBy;
	}
	public void setCreateBy(Integer createBy) {
		this.createBy = createBy;
	}
	public Date getUpdateTime() {
		return updateTime;
	}
	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}
	public Integer getUpdateBy() {
		return updateBy;
	}
	public void setUpdateBy(Integer updateBy) {
		this.updateBy = updateBy;
	}
	public Date getDeleteTime() {
		return deleteTime;
	}
	public void setDeleteTime(Date deleteTime) {
		this.deleteTime = deleteTime;
	}
	public Integer getDeleteBy() {
		return deleteBy;
	}
	public void setDeleteBy(Integer deleteBy) {
		this.deleteBy = deleteBy;
	}
	public String getMemo() {
		return this.memo = memo == null || memo.isEmpty() ? "" : memo;
	}
	public void setMemo(String memo) {
		this.memo = memo == null || memo.isEmpty() ? null : memo;
	}

	@Override
	public boolean equals(Object obj) {
		if (null != obj && obj.getClass() == this.getClass()) {
			Integer eId = ((BaseEntity) obj).getId();
			return null != eId && null != id && id.intValue() == eId.intValue();
		}
		return false;
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(id).append(getClass()).toHashCode();
	}

	@Override
	public String toString() {
		return this.getClass().getName() + ":" + id;
	}
}
